package com.dbq.fast.util;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import com.dbq.fast.R;


public class ProgressDialogUtil {

    protected static ProgressDialog mProgressDialog;
    private static TextView tipTV;// 提示文字

    public static ProgressDialog showProgress(Activity context, String title, String message) {
        return showProgress(context, title, message, false);
    }

    //系统样式
//    private static ProgressDialog showProgress(Context context, String title, String message, int theme) {
//        if (mProgressDialog == null) {
//            if (theme > 0)
//                mProgressDialog = new ProgressDialog(context, theme);
//            else
//                mProgressDialog = new ProgressDialog(context);
//            mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//            mProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
//            mProgressDialog.setCanceledOnTouchOutside(false);// 不能取消
//            mProgressDialog.setCancelable(false);
//            mProgressDialog.setIndeterminate(true);// 设置进度条是否不明确
//        }
//
//        if (!TextUtils.isEmpty(title)) {
//            mProgressDialog.setTitle(title);
//        }
//        mProgressDialog.setMessage(message);
//        mProgressDialog.show();
//        return mProgressDialog;
//    }

    //自定义样式
    public static synchronized ProgressDialog showProgress(Activity context, String title, String message, boolean touchHide) {
        if (context != null && context.isFinishing()) return null;
        if (Build.VERSION.SDK_INT >= 17 && context.isDestroyed()) return null;
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
        View v = LayoutInflater.from(context).inflate(R.layout.loading_dialog, null);// 得到加载view

        tipTV = (TextView) v.findViewById(R.id.tipTextView);
        tipTV.setText(message);// 设置加载信息

        if (mProgressDialog == null) {
            mProgressDialog = new ProgressDialog(context, R.style.loading_dialog);// 创建自定义样式dialog

            mProgressDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
            mProgressDialog.setCanceledOnTouchOutside(touchHide);// 不能取消
            mProgressDialog.setCancelable(touchHide);// 不可以用“返回键”取消
        }

        mProgressDialog.show();
        mProgressDialog.setContentView(v);// 设置布局
        return mProgressDialog;
    }

    public static void hideProgress() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    public static void update(String percent) {
        if (tipTV != null) {
            tipTV.setText(percent);
        }

    }
}
